package com.caiheng.api.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;

public class Base64Util {


    /**
     *  base64字符串转换成图片
     * @param base64
     * @param path
     * @return
     */
    public static void generateImage(String base64, String path,String fileName) {
        // 解密
        try {
            String savePath = path;//"/**/imgtest/";​
            // 去掉base64前缀 data:image/jpeg;base64,
            base64 = base64.substring(base64.indexOf(",", 1) + 1);
            // 解密，解密的结果是一个byte数组
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] imgbytes = decoder.decode(base64);
            for (int i = 0; i < imgbytes.length; ++i) {
                if (imgbytes[i] < 0) {
                    imgbytes[i] += 256;
                }
            }

            // 保存图片
            OutputStream out = new FileOutputStream(savePath.concat(fileName));
            out.write(imgbytes);
            out.flush();
            out.close();
            // 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
//            return imgClassPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
